package com.delivery.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class PagePoint extends View {

	private String TAG = "PagePoint";

	Paint p;

	int totalPage;// 总页面数

	int currentPage;

	Bitmap bmp, bmp2;
	int screenW;

	int screenH;

	boolean flag = false;

	int ws;// 图片的宽

	int dw = 10;// 点之间的间距

	int totalWidth;

	int startX;

	public PagePoint(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		p = new Paint();

		p.setFlags(Paint.ANTI_ALIAS_FLAG);

		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

		screenW = displayMetrics.widthPixels;

		screenH = displayMetrics.heightPixels;

		//bmp = BitmapFactory.decodeResource(getResources(), R.drawable.click);// 只读,不能直接在bmp上画

		//bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.clicked);// 只读,不能直接在bmp上画

		ws = bmp.getWidth();

		dw = 10;

	}

	public PagePoint(Context context, AttributeSet attrs) {

		this(context, attrs, 0);

	}

	public PagePoint(Context context) {

		this(context, null);

	}

	public void addPagePoint(int number) {

		totalPage = number;

		currentPage = 0;

	}

	public void toPagePoint(int currentPage, int nextOrPre) {

		flag = true;

		this.currentPage = nextOrPre;

		totalWidth = ws * totalPage + dw * (totalPage - 1);

		startX = (screenW - totalWidth) / 2;

		invalidate(startX, screenH - 213 - ws, startX + (ws + dw) * totalPage,
				screenH);

		// invalidate();

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		Log.i(TAG, "----------onTouchEvent----------");

		return super.onTouchEvent(event);

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		Log.i(TAG, "----------onMeasure----------");

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	protected void onDraw(Canvas canvas) {

		super.onDraw(canvas);

		Log.i(TAG, "----------onDraw----------");

		totalWidth = ws * totalPage + dw * (totalPage - 1);

		startX = (screenW - totalWidth) / 2;// 点的起始位置

		for (int i = 0; i < totalPage; i++) {

			Log.i(TAG, "startX" + (startX + (ws + dw) * i)
					+ "----------onDraw----------");

			canvas.drawBitmap(currentPage == i ? bmp2 : bmp, startX + (ws + dw)
					* i, screenH - 213 + ws / 2, p);

		}

	}

}
